<?php
require_once('config/config.php');
require_once('checked.php'); //判断是否登录

//删除功能
if($_POST){
    $jobid = isset($_POST['jobid']) ? $_POST['jobid'] :'0';
    $success = [
        'result'=> false,
        'msg'=> ''
    ];
    $row = del("job","id IN ($jobid)");
    if($row){
        $success['result'] = true;
        $success['msg'] = '删除成功!';
    }else{
        $success['result'] = false;
        $success['msg'] = '删除失败!';
    }
    echo json_encode($success);
    exit;
}

//获取当前页码
$page = isset($_GET['page'])?trim($_GET['page']):1;
// 记录总的页数
$sql = "SELECT COUNT(*) FROM {$pre_}job";
$count =  select($sql);

$count = isset($count["COUNT(*)"]) ? (int)$count["COUNT(*)"] :0;
//每个页面显示的数量 
$limit = 3;
//显示几页
$size = 3;

$pageStr =  page($page,$count,$limit,$size,"scott");
//查询语句的开始页码
$startPag = ($page-1)*$limit;

// 书写sql语句
$sql = "SELECT * FROM {$pre_}job ORDER BY id ASC LIMIT {$startPag},{$limit}";

// 接受返回的数组
$list = selectAll($sql);

?>


<!DOCTYPE html>
<html lang="en">
    <head>
       <?php require_once('meta.php') ?>
       <link rel="stylesheet" href="./assets/css/page.css">
    </head>
    <body>
        <!-- 头部 -->
        <?php require_once('header.php') ?>

        <div class="sidebar-nav">
            <a href="#dashboard-menu" class="nav-header" data-toggle="collapse"><i class="icon-dashboard"></i>控制面板</a>
            <ul id="dashboard-menu" class="nav nav-list collapse in">
                <li><a href="index.php">首页</a></li>
                <li ><a href="list.php">管理页面</a></li>
            </ul>
        </div>

        <div class="content">
            <div class="header">
                <h1 class="page-title">职务列表</h1>
            </div>
            <ul class="breadcrumb">
                <li><a href="index.php">Home</a> <span class="divider">/</span></li>
                <li class="active">Index</li>
            </ul>

            <div class="container-fluid">
                <div class="row-fluid">
                    <div class="btn-toolbar">
                        <a class="btn btn-primary" href="JobAdd.php"><i class="icon-plus"></i>添加职务</a>
                        <a href="#myModal" onclick="DeleteAll()" class="btn btn-danger" role="button" data-toggle="modal"><i class="icon-remove"></i></i>批量删除</a>
                    </div>
                    <div class="well">
                        <table class="table">
                            <thead>
                            <tr>
                                <th><input type="checkbox" id="action"></th>
                                <th>ID</th>
                                <th>职务名称</th>
                                <th>操作</th>
                                <th style="width: 26px;"></th>
                            </tr>
                            </thead>
                            <tbody>
                            <?php foreach($list as $value) {?>
                            <tr>
                            <td><input type="checkbox" name="del[]" value="<?php echo $value['id'];?>" /></td>
                                <td><?php echo $value['id']?></td>
                                <td><?php echo $value['name']?></td>
                                <td>
                                    <a href="JobEdit.php?jobid=<?php echo $value['id'] ?>"><i class="icon-pencil"></i></a>
                                    <a href="#myModal" onclick="DeleteOne(this)" data-pid="<?php echo $value['id'] ?>" role="button" data-toggle="modal"><i class="icon-remove"></i></a>
                                </td>
                            </tr>
                            <?php }?>
                            </tbody>
                        </table>
                    </div>
                    <div class="pagination">
                        <?php echo $pageStr; ?>
                    </div>
                    <!-- modal弹出框 -->
                    <div class="modal small hide fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
                        <div class="modal-header">
                            <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                            <h3 id="myModalLabel">删除提醒</h3>
                        </div>
                        <div class="modal-body">
                            <p class="error-text"><i class="icon-warning-sign modal-icon"></i>确认删除吗?</p>
                        </div>
                        <div class="modal-footer">
                            <button class="btn" data-dismiss="modal" aria-hidden="true">取消</button>
                            <button class="btn btn-danger" data-dismiss="modal" id="delete">确定</button>
                        </div>
                    </div>

                    <footer>
                        <hr>
                        <p>&copy; 2017 <a href="#" target="_blank">copyright</a></p>
                    </footer> 
                </div>
            </div>
        </div>
    </body>
</html>

<script>
    // 定义一个存放id的数组
    var listId = [];


    // 批量删除多选
    $("#action").click(function(){
        $("input[name='del[]']").prop('checked',$(this).prop('checked'))
    })

    // 触发单次点击的事件
    function DeleteOne(that){
        listId = [];
        //获取自定义属性的value值
        var id = that.dataset.pid;
        
        //判断元素是否在当前数组当中 如果不存在在追加
        if(!listId.includes(id))listId = [id];

        // console.log(listId)

    }
    //触发批量删除事件
    function DeleteAll(){
        //如果不清空的话 会出现某次删除失败 下次不选中也能删除
        listId = [];//清空
        // each循环
        $("input[name='del[]']:checked").each((key,values)=>{
            // console.log(key,values.value)
            if(!listId.includes(values.value))listId.push(values.value)
        })
        // console.log(listId)
    }
    //最终删除效果
    $("#delete").click(function(){
        if(listId.length<=0){
            alert('没有选中需要删除的数据，你删除啥!');
            return false;
        }
        //ajax请求
        $.ajax({
            type:'post',
            data:{jobid:listId.join(',')},
            dataType:'json',
            success:(success)=>{
                if(success.result){
                    setTimeout(()=>{
                        location.href = 'JobList.php';
                    },500);
                    return false;
                }else{
                    alert('删除失败!');
                    return false;
                }
            },
            error:(error)=>{
                console.log(error)
            }
        })
    })
</script>